package com.zhou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.api.dto.InventoryDTO;
import com.zhou.api.entity.Inventory;
import com.zhou.mapper.InventoryMapper;
import com.zhou.service.InventoryService;
import com.zhou.utils.BeanChangeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.zhou.utils.CommonCast.YES_STR;

/**
 * @Description :
 * @Author : zyy
 * @Date: 2024/8/22  15:09
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
    @Resource
    private InventoryMapper inventoryMapper;

    /**
     * 添加入库
     * @param dto
     */
    @Override
    public void add(InventoryDTO dto) {
        Inventory inventory = new Inventory();
        BeanUtil.copyProperties(dto, inventory);
        if (BeanChangeUtils.isNotEmpty(inventory.getWarehouseId())) {
            inventoryMapper.insert(inventory);
        }
    }

    /**
     * 修改出库
     *
     * @param id
     */
    @Override
    public void update(String id) {
        Inventory inventory = inventoryMapper.selectById(id);
        if (BeanChangeUtils.isNotEmpty(inventory)) {
            inventory.setInventoryStatus(YES_STR);
            inventoryMapper.updateById(inventory);
        }
    }

    /**
     * @param id
     */
    @Override
    public void delete(String id) {
        Inventory inventory = inventoryMapper.selectById(id);
        if (BeanChangeUtils.isNotEmpty(inventory)) {
            inventory.setDelFlag(YES_STR);
            inventoryMapper.updateById(inventory);
        }
    }

    /**
     * @param page
     * @param delFlag
     * @param inventoryStatus
     * @param status
     * @return
     */
    @Override
    public Page<Inventory> getList(Page page, String delFlag, String inventoryStatus, String status) {
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(delFlag)) {
            queryWrapper.eq(Inventory::getDelFlag, delFlag);
        }
        if (ObjectUtil.isNotEmpty(inventoryStatus)) {
            queryWrapper.eq(Inventory::getInventoryStatus, inventoryStatus);
        }
        if (ObjectUtil.isNotEmpty(status)) {
            queryWrapper.eq(Inventory::getStatus, status);
        }

        return inventoryMapper.selectPage(page, queryWrapper);
    }

    /**
     * 审核
     * @param s
     * @return
     */
    @Override
    public Inventory examine(String s) {
        return inventoryMapper.selectById(s);
    }

    /**
     * @param list
     */
    @Override
    public void updateList(List<Inventory> list) {
        inventoryMapper.updateList(list);
    }
}
